package com.syni.mdd.yhd.common.all.listen.businessClaim;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.entity.BmsAgent;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessFlow;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessAuth;
import com.syni.mdd.yhd.common.all.entity.cdn.BmsCdnData;
import com.syni.mdd.yhd.common.all.entity.flow.FlowDocument;
import com.syni.mdd.yhd.common.all.service.BmsAgentService;
import com.syni.mdd.yhd.common.all.service.BmsBusinessFlowService;
import com.syni.mdd.yhd.common.all.service.BmsPazarConditionService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class BusinessClaimListener implements ApplicationListener<BusinessClaimEvent> {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsAgentService bmsAgentService;

    @Autowired
    BmsPazarConditionService bmsPazarConditionService;

    @Override
    @Async
    public void onApplicationEvent(BusinessClaimEvent businessClaimEvent) {
        String invitationCode = businessClaimEvent.getInvitationCode();
        BmsUserBusinessAuth bmsUserBusinessAuth = businessClaimEvent.getBmsUserBusinessAuth();
        BmsAgent bmsAgent = null;
        if(StringUtils.isNotBlank(invitationCode)) {
            List<BmsAgent> bmsAgents = bmsAgentService.findByInvitationCode(invitationCode);
            if (bmsAgents != null && bmsAgents.size() > 0) {
                bmsAgent = bmsAgents.get(0);
            }
        }
        //创建一个店铺认领记录
        bmsPazarConditionService.addPazarCondition(bmsUserBusinessAuth,bmsAgent);
    }
}
